home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1995 October / EnigmA AMIGA RUN 01 (1995)(G.R. Edizioni)(IT)[!][issue 1995-10][Aminet 7].iso / Aminet / comm / tcp / MUIAdt.lha / src / adt.c next >
C/C++ Source or Header  |  1994-09-01  |  39KB  |  1,325 lines

  1. /*
  2.  *  Yes, yes, yes. No running commentary throughout the code, but I understand it
  3.  *   (I think)
  4.  *   (Yeah, have no fear, I remember now)
  5.  */
  6.  
  7. void __regargs __chkabort(void);
  8. void __regargs __chkabort(void) {}
  9.  
  10. #include "adt.h"
  11.  
  12. LONG __stack = 8192;
  13.  
  14. #define FILE_PAGE   0
  15. #define README_PAGE 1
  16. #define GET_PAGE    2
  17.  
  18. #define FILE_NEW_PAGE     3
  19. #define FILE_RECENT_PAGE 0
  20. #define FILE_ALL_PAGE     1
  21. #define FILE_TAGGED_PAGE 2
  22.  
  23. struct Library *MUIMasterBase, *childSocketBase;
  24.  
  25. static APTR win, hosts_win, add_host_win, config_win;
  26. static APTR mainpage, lvpage, recentlv, shortlv, taggedlv, newlv, hostslv, readtext, getlv;
  27. static APTR adt_statusobj, read_next_but, read_can_but, get_abort_but;
  28. static APTR host_add_but, host_del_but, host_up_but, host_down_but, host_cancel_but, host_connect_but;
  29. static APTR host_add_country, host_add_site, host_add_dir, host_add_done;
  30. APTR get_gauge, app;
  31. static APTR cfg_but, sort_but, view_but, read_but, name_but, get_but, host_but, quit_but;
  32. static APTR search_str, search_next, search_can;
  33. static APTR def_sort_but, def_view_but, def_getdir, def_tmpdir, def_uncomp, def_done;
  34. static LONG sorttype=0, viewtype=0;
  35.  
  36. static LONG readpos;
  37.  
  38. static BOOL gotrecent=FALSE, gotshort=FALSE, gotnew=FALSE;
  39. static BOOL sortrecent=TRUE, sortshort=TRUE, sortnew=TRUE, sorttagged=TRUE;
  40. extern BOOL connected;
  41.  
  42. struct HostLineRec *host=NULL;
  43. struct PrefsRec prefs;
  44.  
  45. static const char *SortEntries[] = { "Name", "Age", "Dir", NULL };
  46. static const char *ViewEntries[] = { "New", "Recent", "All", "Tagged", NULL };
  47.  
  48. /*************************/
  49. /* Init & Fail Functions */
  50. /*************************/
  51.  
  52. static VOID fail(APTR app,char *str)
  53. {
  54.     if (app)
  55.         MUI_DisposeObject(app);
  56.  
  57.     if (MUIMasterBase)
  58.         CloseLibrary(MUIMasterBase);
  59.  
  60.     if (str)
  61.     {
  62.         puts(str);
  63.         exit(20);
  64.     }
  65.     exit(0);
  66. }
  67.  
  68. static VOID init(VOID)
  69. {
  70.     if (!(MUIMasterBase = OpenLibrary(MUIMASTER_NAME,MUIMASTER_VMIN)))
  71.         fail(NULL,"Failed to open "MUIMASTER_NAME".");
  72. }
  73.  
  74.  
  75. /*
  76.  * Breaks a string at the token 'mark', returning a pointer to the begining
  77.  * of the string (from the adt1.3 distribution)
  78.  */
  79.  
  80. char *gettok(char **ptr, char mark, int *toklen)
  81. {
  82.   char *t, *start = *ptr;
  83.  
  84.   if( (t=(char*)strchr(*ptr,mark))) {
  85.     *t=0;
  86.     *ptr=t+1;
  87.   }
  88.   *toklen = t - start + 1;
  89.   return start;
  90. }
  91.  
  92.  
  93. /*
  94.  *  File listview hooks
  95.  */
  96.  
  97.  
  98. __saveds __asm struct FileLineRec *file_const_func(REG(a0) struct Hook *hook,
  99.                            REG(a2) APTR mem_pool,
  100.                            REG(a1) char *line)
  101. {
  102.     register struct FileLineRec *flr;
  103.     char *ln = line;
  104.     LONG fsize;
  105.     register char *dir, *name, *readme, *descr;
  106.     int dirlen, namelen, readmelen, descrlen, nothanks;
  107.  
  108.     flr = (struct FileLineRec *)AllocMem(sizeof(struct FileLineRec), MEMF_ANY);
  109.  
  110.     sscanf(gettok(&ln, '@', ¬hanks), "%lx", &flr->time);
  111.     if (flr->time > prefs.thistime)
  112.     prefs.thistime = flr->time;
  113.  
  114.     dir = gettok(&ln, '@', &dirlen);
  115.     name = gettok(&ln, ';', &namelen);
  116.     sscanf(gettok(&ln, '@', ¬hanks), "%lx", &fsize);
  117.     readme = gettok(&ln, '@', &readmelen);
  118.     descr = gettok(&ln, '\n', &descrlen);
  119.  
  120.     flr->recsize = dirlen + namelen + 5 + readmelen + descrlen;
  121.     flr->dir = AllocMem(flr->recsize, MEMF_ANY|MEMF_CLEAR);
  122.     flr->name = flr->dir + dirlen;
  123.     flr->size = flr->name + namelen;
  124.     flr->readme = flr->size + 5;
  125.     flr->descr = flr->readme + readmelen;
  126.  
  127.     strcpy(flr->dir, dir);
  128.     strcpy(flr->name, name);
  129.     flr->intsize = fsize;
  130.     if (fsize < 1000) sprintf(flr->size, "%dB", fsize);
  131.     else if (fsize < 1000000) sprintf(flr->size, "%dK", fsize / 1024);
  132.     else if (fsize < 10000000) sprintf(flr->size, "%.1fM", fsize / 1048576.0);
  133.     else if (fsize < 1000000000) sprintf(flr->size, "%dM", fsize / 1048576);
  134.     else strcpy(flr->size, "big");
  135.     strcpy(flr->readme, readme);
  136.     strcpy(flr->descr, descr);
  137.  
  138.     if(flr->readme[strlen(flr->readme)-1] == ';') {
  139.     flr->readme[strlen(flr->readme)-1] = '\0';
  140.     flr->flags |= FLAG_README;
  141.     }
  142.  
  143.     return(flr);
  144. }
  145.  
  146. static struct Hook file_const_hook = {
  147.     {NULL, NULL},
  148.     (void *)file_const_func,
  149.     NULL, NULL
  150. };
  151.  
  152. __saveds __asm LONG file_dest_func(REG(a0) struct Hook *hook,
  153.                    REG(a2) APTR mem_pool,
  154.                    REG(a1) struct FileLineRec *flr)
  155. {
  156.     FreeMem(flr->dir, flr->recsize);
  157.     FreeMem(flr, sizeof(struct FileLineRec));
  158.     return(0);
  159. }
  160.  
  161. static struct Hook file_dest_hook = {
  162.     {NULL, NULL},
  163.     (void *)file_dest_func,
  164.     NULL, NULL
  165. };
  166.  
  167. __saveds __asm LONG file_disp_func(REG(a0) struct Hook *hook,
  168.                    REG(a2) char *array[],
  169.                    REG(a1) struct FileLineRec *flinerec)
  170. {
  171.     if (flinerec) {
  172.     if (HAS_README(flinerec)) array[0] = "*";
  173.         else array[0] = "";
  174.     array[1] = flinerec->name;
  175.     array[2] = flinerec->dir;
  176.     array[3] = flinerec->size;
  177.     array[4] = flinerec->descr;
  178.     } else {
  179.     array[0] = "";
  180.     array[1] = "\033c\0338Name";
  181.     array[2] = "\033c\0338Dir";
  182.     array[3] = "\033c\0338Size";
  183.     array[4] = "\033c\0338Description";
  184.    }
  185.    return(0);
  186. }
  187.  
  188. static struct Hook file_disp_hook = {
  189.      {NULL, NULL},
  190.      (void *)file_disp_func,
  191.      NULL, NULL
  192. };
  193.  
  194. __saveds __asm LONG file_cmp_func(REG(a1) struct FileLineRec *line1,
  195.                   REG(a2) struct FileLineRec *line2)
  196. {
  197.     switch(sorttype) {
  198.     case 0:
  199.         return(stricmp(line1->name, line2->name));
  200.         break;
  201.  
  202.     case 1:
  203.         if ((line1->time - line2->time) < 0)
  204.         return(1);
  205.         else if ((line1->time - line2->time) > 0)
  206.         return(-1);
  207.         else return(0);
  208.         break;
  209.  
  210.     case 2:
  211.         return(stricmp(line1->dir, line2->dir));
  212.         break;
  213.     }
  214. }
  215.  
  216. static struct Hook file_cmp_hook = {
  217.     {NULL, NULL},
  218.     (void *)file_cmp_func,
  219.     NULL, NULL
  220. };
  221.  
  222.  
  223. /*
  224.  *  Host listview hooks
  225.  */
  226.  
  227. __saveds __asm struct HostLineRec *host_const_func(REG(a0) struct Hook *hook,
  228.                            REG(a2) APTR mem_pool,
  229.                            REG(a1) char *line)
  230. {
  231.     register struct HostLineRec *hlr;
  232.     register char *country, *site;
  233.     char *ln=line;
  234.     int dummy;
  235.  
  236.     hlr = (struct HostLineRec *)AllocMem(sizeof(struct HostLineRec), MEMF_ANY);
  237.  
  238.     country = gettok(&ln, ':', &dummy);
  239.     site = gettok(&ln, ':', &dummy);
  240.     if (country == NULL || site == NULL)
  241.        return(NULL);
  242.     strcpy(hlr->country, country);
  243.     strcpy(hlr->host, site);
  244.     strcpy(hlr->dir, ln);
  245.     return(hlr);
  246. }
  247.  
  248. static struct Hook host_const_hook = {
  249.     {NULL, NULL},
  250.     (void *)host_const_func,
  251.     NULL, NULL
  252. };
  253.  
  254. __saveds __asm LONG host_dest_func(REG(a0) struct Hook *hook,
  255.                    REG(a2) APTR mem_pool,
  256.                    REG(a1) struct HostLineRec *hlinerec)
  257. {
  258.     FreeMem(hlinerec, sizeof(struct HostLineRec));
  259.     return(0);
  260. }
  261.  
  262. static struct Hook host_dest_hook = {
  263.     {NULL, NULL},
  264.     (void *)host_dest_func,
  265.     NULL, NULL
  266. };
  267.  
  268. __saveds __asm LONG host_disp_func(REG(a0) struct Hook *hook,
  269.                    REG(a2) char *array[],
  270.                    REG(a1) struct HostLineRec *hlinerec)
  271. {
  272.     if (hlinerec) {
  273.     array[0] = hlinerec->country;
  274.     array[1] = hlinerec->host;
  275.     array[2] = hlinerec->dir;
  276.     } else {
  277.     array[0] = "\033c\0338Country";
  278.     array[1] = "\033c\0338Host";
  279.     array[2] = "\033c\0338Directory";
  280.     }
  281.     return(0);
  282. }
  283.  
  284. static struct Hook host_disp_hook = {
  285.     {NULL, NULL},
  286.     (void *)host_disp_func,
  287.     NULL, NULL
  288. };
  289.  
  290. __saveds __asm LONG host_cmp_func(REG(a1) struct HostLineRec *line1,
  291.                   REG(a2) struct HostLineRec *line2)
  292. {
  293.     return(stricmp(line1->host, line2->host));
  294. }
  295.  
  296. static struct Hook host_cmp_hook = {
  297.     {NULL, NULL},
  298.     (void *)host_cmp_func,
  299.     NULL, NULL
  300. };
  301.  
  302. main(int argc, char *argv[])
  303. {
  304.     BOOL running = TRUE;
  305.  
  306. /* Initialise default prefs. */
  307.     prefs.defsort = 0;
  308.     prefs.defview = 0;
  309.     strcpy(prefs.getdir, "RAM:");
  310.     strcpy(prefs.tmpdir, "RAM:");
  311.     strcpy(prefs.defcfg, "ENV:MUIAdt.prefs");
  312.     strcpy(prefs.uncomp, "uncompress");
  313.     prefs.server[0] = '\0';
  314.     prefs.lasttime = 0;
  315.     prefs.thistime = 0;
  316.  
  317. /* Check for bsdsocket.library */
  318.     if (childSocketBase = OpenLibrary("bsdsocket.library", 2L))
  319.     CloseLibrary(childSocketBase);
  320.     else
  321.     exit(20);
  322.  
  323.     init();
  324.  
  325.     app = ApplicationObject,
  326.       MUIA_Application_Title,    "MUIAdt",
  327.       MUIA_Application_Version,    "$VER: MUIadt 1.3 (1/9/94)",
  328.       MUIA_Application_Copyright,    "© 1994 by Jem Atahan",
  329.       MUIA_Application_Author,    "Jem Atahan",
  330.       MUIA_Application_Description, "AmiNet Download Tool",
  331.       MUIA_Application_Base,    "MADT",
  332.  
  333.       SubWindow, win = WindowObject,
  334.           MUIA_Window_ID, MAKE_ID('M','A','I','N'),
  335.           MUIA_Window_Title, "MUIAdt",
  336.           MUIA_Window_Width, MUIV_Window_Width_Visible(80),
  337.           MUIA_Window_Height, MUIV_Window_Height_Visible(60),
  338.  
  339. /* Main Window */
  340.           WindowContents, mainpage = PageGroup,
  341.  
  342. /* Files Page */
  343.           Child, VGroup,
  344.               Child, lvpage = PageGroup,
  345.               Child, recentlv = ListviewObject,
  346.                   MUIA_Listview_MultiSelect, TRUE,
  347.                   MUIA_Listview_DoubleClick, TRUE,
  348.                   MUIA_Listview_List, ListObject,
  349.                   InputListFrame,
  350.                   MUIA_List_Format, "W=1,W=200,W=50,W=10 P=\033r,W=600",
  351.                   MUIA_List_Title, TRUE,
  352.                   MUIA_List_ConstructHook, &file_const_hook,
  353.                   MUIA_List_DestructHook, &file_dest_hook,
  354.                   MUIA_List_CompareHook, &file_cmp_hook,
  355.                   MUIA_List_DisplayHook, &file_disp_hook,
  356.                   End,
  357.                   End,
  358.               Child, shortlv = ListviewObject,
  359.                   MUIA_Listview_MultiSelect, TRUE,
  360.                   MUIA_Listview_DoubleClick, TRUE,
  361.                   MUIA_Listview_List, ListObject,
  362.                   InputListFrame,
  363.                   MUIA_List_Format, "W=1,W=200,W=50,W=10 P=\033r,W=600",
  364.                   MUIA_List_Title, TRUE,
  365.                   MUIA_List_ConstructHook, &file_const_hook,
  366.                   MUIA_List_DestructHook, &file_dest_hook,
  367.                   MUIA_List_CompareHook, &file_cmp_hook,
  368.                   MUIA_List_DisplayHook, &file_disp_hook,
  369.                   End,
  370.                   End,
  371.               Child, taggedlv = ListviewObject,
  372.                   MUIA_Listview_MultiSelect, TRUE,
  373.                   MUIA_Listview_DoubleClick, TRUE,
  374.                   MUIA_Listview_List, ListObject,
  375.                   InputListFrame,
  376.                   MUIA_List_Format, "W=1,W=200,W=50,W=10 P=\033r,W=600",
  377.                   MUIA_List_Title, TRUE,
  378.                   MUIA_List_ConstructHook, &file_const_hook,
  379.                   MUIA_List_DestructHook, &file_dest_hook,
  380.                   MUIA_List_CompareHook, &file_cmp_hook,
  381.                   MUIA_List_DisplayHook, &file_disp_hook,
  382.                   End,
  383.                   End,
  384.               Child, newlv = ListviewObject,
  385.                   MUIA_Listview_MultiSelect, TRUE,
  386.                   MUIA_Listview_DoubleClick, TRUE,
  387.                   MUIA_Listview_List, ListObject,
  388.                   InputListFrame,
  389.                   MUIA_List_Format, "W=1,W=200,W=50,W=10 P=\033r,W=600",
  390.                   MUIA_List_Title, TRUE,
  391.                   MUIA_List_ConstructHook, &file_const_hook,
  392.                   MUIA_List_DestructHook, &file_dest_hook,
  393.                   MUIA_List_CompareHook, &file_cmp_hook,
  394.                   MUIA_List_DisplayHook, &file_disp_hook,
  395.                   End,
  396.                   End,
  397.               End,
  398.               Child, HGroup,
  399.               MUIA_Weight, 1,
  400.               Child, KeyLabel1("Sort by:", 's'), Child, sort_but = KeyCycle(SortEntries, 's'),
  401.               Child, KeyLabel1("View:", 'v'), Child, view_but = KeyCycle(ViewEntries, 'v'),
  402.               Child, VSpace(0),
  403.               Child, read_but = KeyButton("Readme", 'r'),
  404.               Child, name_but = PopobjectObject,
  405.                   MUIA_Popstring_String, Label1(""),
  406.                   MUIA_Popstring_Button, KeyButton("Name", 'n'),
  407.                   MUIA_Popobject_Object, VGroup,
  408.                   GroupFrame,
  409.                   Child, KeyLabel1("\33cSearch for:", 'r'),
  410.                   Child, search_str = KeyString("", 256, 'r'),
  411.                   Child, search_next = KeyButton("Next", 'x'),
  412.                   Child, search_can = KeyButton("Cancel", 'a'),
  413.                   End,
  414.                   End,
  415.               Child, get_but = KeyButton("Download", 'd'),
  416.               Child, host_but = KeyButton("Host", 'h'),
  417.               Child, cfg_but = KeyButton("Config", 'c'),
  418.               Child, quit_but = KeyButton("Quit", 'q'),
  419.               End,
  420.               Child, adt_statusobj = TextObject,
  421.               TextFrame,
  422.               MUIA_Background, MUII_TextBack,
  423.               MUIA_Text_PreParse, "\033c\0338",
  424.               End,
  425.               End,
  426.  
  427. /* Readme Page */
  428.           Child, VGroup,
  429.               Child, TextObject,
  430.              TextFrame,
  431.               MUIA_Background, MUII_TextBack,
  432.               MUIA_Text_PreParse, "\033c\033b\0338",
  433.               MUIA_Text_Contents, "Readme file",
  434.               End,
  435.               Child, ListviewObject,
  436.               MUIA_Weight, 200,
  437.               MUIA_Listview_Input, FALSE,
  438.               MUIA_Listview_List, readtext = FloattextObject,
  439.                   MUIA_Frame, MUIV_Frame_ReadList,
  440.                   MUIA_Background, MUII_TextBack,
  441.                   MUIA_Floattext_TabSize, 4,
  442.                   MUIA_Floattext_Justify, TRUE,
  443.                   End,
  444.               End,
  445.               Child, HGroup,
  446.               Child, read_next_but = KeyButton("Next", 'x'),
  447.               Child, read_can_but = KeyButton("Cancel", 'c'),
  448.               Child, HSpace(13),
  449.               End,
  450.               End,
  451.  
  452. /* Downloading Page */
  453.           Child, VGroup,
  454.               Child, TextObject,
  455.               TextFrame,
  456.               MUIA_Background, MUII_TextBack,
  457.               MUIA_Text_PreParse, "\033c\033b\0338",
  458.               MUIA_Text_Contents, "Downloading",
  459.               End,
  460.               Child, getlv = ListviewObject,
  461.               MUIA_Listview_Input, FALSE,
  462.               MUIA_Listview_DoubleClick, FALSE,
  463.               MUIA_Listview_List, ListObject,
  464.                   InputListFrame,
  465.                   MUIA_List_Format, "W=1,W=200,W=50,W=10 P=\033r,W=600",
  466.                   MUIA_List_Title, TRUE,
  467.                   MUIA_List_ConstructHook, &file_const_hook,
  468.                   MUIA_List_DestructHook, &file_dest_hook,
  469.                   MUIA_List_CompareHook, &file_cmp_hook,
  470.                   MUIA_List_DisplayHook, &file_disp_hook,
  471.                   End,
  472.               End,
  473.               Child, HGroup,
  474.               Child, VGroup,
  475.                   MUIA_Weight, 50,
  476.                   Child, get_abort_but = KeyButton("Abort", 'a'),
  477.                   Child, VSpace(13),
  478.                   End,
  479.               Child, VGroup,
  480.                   GroupSpacing(1),
  481.                   MUIA_Weight, 400,
  482.                   Child, get_gauge = GaugeObject,
  483.                   GaugeFrame,
  484.                   MUIA_Gauge_Horiz, TRUE,
  485.                   End,
  486.                   Child, ScaleObject,
  487.                   MUIA_Scale_Horiz, TRUE,
  488.                   End,
  489.                   End,
  490.               Child, HSpace(13),
  491.               End,
  492.               End,
  493.           End,
  494.           End,
  495.  
  496. /* Hosts Window */
  497.       SubWindow, hosts_win = WindowObject,
  498.           MUIA_Window_ID, MAKE_ID('H','O','S','T'),
  499.           MUIA_Window_Title, "MUIAdt Hosts",
  500.           MUIA_Window_Width, MUIV_Window_Width_Visible(60),
  501.           MUIA_Window_Height, MUIV_Window_Height_Visible(35),
  502.           WindowContents, HGroup,
  503.           Child, hostslv = ListviewObject,
  504.               MUIA_Weight, 400,
  505.               MUIA_Listview_MultiSelect, FALSE,
  506.               MUIA_Listview_DoubleClick, TRUE,
  507.               MUIA_Listview_List, ListObject,
  508.               InputListFrame,
  509.               MUIA_List_Format, ",,",
  510.               MUIA_List_Title, TRUE,
  511.               MUIA_List_ConstructHook, &host_const_hook,
  512.               MUIA_List_DestructHook, &host_dest_hook,
  513.               MUIA_List_CompareHook, &host_cmp_hook,
  514.               MUIA_List_DisplayHook, &host_disp_hook,
  515.               End,
  516.               End,
  517.           Child, VGroup,
  518.               MUIA_Weight, 1,
  519.               Child, VSpace(0),
  520.               Child, host_add_but = KeyButton("Add", 'a'),
  521.               Child, host_del_but = KeyButton("Delete", 'l'),
  522. /*            Child, host_up_but = KeyButton("Up", 'u'),
  523.               Child, host_down_but = KeyButton("Down", 'd'),*/
  524.               Child, VSpace(0),
  525.               Child, host_connect_but = KeyButton("Connect", 'c'),
  526.               Child, host_cancel_but = KeyButton("Cancel", 'n'),
  527.               Child, VSpace(0),
  528.               End,
  529.           End,
  530.           End,
  531.  
  532. /* Add Host Window */
  533.       SubWindow, add_host_win = WindowObject,
  534.           MUIA_Window_ID, MAKE_ID('A','D','D','H'),
  535.           MUIA_Window_Title, "Add a host",
  536.           WindowContents, VGroup,
  537.           Child, HGroup,
  538.               Child, KeyLabel1("Country", 'c'),
  539.               Child, host_add_country = StringObject,
  540.               StringFrame,
  541.               MUIA_ControlChar, 'c',
  542.               MUIA_String_MaxLen, 30,
  543.               MUIA_String_Reject, ":",
  544.               End,
  545.               End,
  546.           Child, HGroup,
  547.               Child, KeyLabel1("Site", 's'),
  548.               Child, host_add_site = StringObject,
  549.               StringFrame,
  550.               MUIA_ControlChar, 's',
  551.               MUIA_String_MaxLen, 30,
  552.               MUIA_String_Reject, ":",
  553.               End,
  554.               End,
  555.           Child, HGroup,
  556.               Child, KeyLabel1("Directory", 'r'),
  557.               Child, host_add_dir = StringObject,
  558.               StringFrame,
  559.               MUIA_ControlChar, 'r',
  560.               MUIA_String_MaxLen, 30,
  561.               MUIA_String_Reject, ":",
  562.               End,
  563.                End,
  564.           Child, host_add_done = KeyButton("Done", 'd'),
  565.           End,
  566.           End,
  567. /* Config Window */
  568.       SubWindow, config_win = WindowObject,
  569.           MUIA_Window_ID, MAKE_ID('C','O','N','F'),
  570.           MUIA_Window_Title, "MUIAdt Config Options",
  571.           WindowContents, VGroup,
  572.           Child, HGroup, Child, KeyLabel1("Default Sort by:", 's'), Child, def_sort_but = KeyCycle(SortEntries, 's'), End,
  573.           Child, HGroup, Child, KeyLabel1("Default View by:", 'v'), Child, def_view_but = KeyCycle(ViewEntries, 'v'), End,
  574.           Child, HGroup,
  575.               Child, KeyLabel1("Download Directory:", 'd'),
  576.               Child, def_getdir = PopaslObject,
  577.               MUIA_Popstring_String, KeyString("", 256, 'd'),
  578.               MUIA_Popstring_Button, PopButton(MUII_PopDrawer),
  579.               ASLFR_TitleText  , "Please select a drawer...",
  580.               ASLFR_DrawersOnly, TRUE,
  581.               End,
  582.               End,
  583.           Child, HGroup,
  584.               Child, KeyLabel1("Temporary Directory:", 't'),
  585.               Child, def_tmpdir = PopaslObject,
  586.               MUIA_Popstring_String, KeyString("", 256, 't'),
  587.               MUIA_Popstring_Button, PopButton(MUII_PopDrawer),
  588.               ASLFR_TitleText  , "Please select a drawer...",
  589.               ASLFR_DrawersOnly, TRUE,
  590.               End,
  591.               End,
  592.           Child, HGroup,
  593.               Child, KeyLabel1("Uncompress Command:", 'u'),
  594.               Child, def_uncomp = KeyString("", 256, 'u'),
  595.               End,
  596.           Child, HGroup, Child, HSpace(0), Child, def_done = KeyButton("Done", 'o'), Child, HSpace(0), End,
  597.           End,
  598.           End,
  599.       End;
  600.     if (!app) fail(app, "Couldn't create application.");
  601.  
  602. /* Respond to option button presses */
  603.     DoMethod(sort_but, MUIM_Notify, MUIA_Cycle_Active, MUIV_EveryTime, app, 2, MUIM_Application_ReturnID, ID_Sort);
  604.     DoMethod(view_but, MUIM_Notify, MUIA_Cycle_Active, MUIV_EveryTime, app, 2, MUIM_Application_ReturnID, ID_View);
  605.     DoMethod(read_but, MUIM_Notify, MUIA_Pressed, FALSE, app, 2, MUIM_Application_ReturnID, ID_Readme);
  606.     DoMethod(get_but, MUIM_Notify, MUIA_Pressed, FALSE, app, 2, MUIM_Application_ReturnID, ID_Get);
  607.     DoMethod(host_but, MUIM_Notify, MUIA_Pressed, FALSE, hosts_win, 3, MUIM_Set, MUIA_Window_Open, TRUE);
  608.     DoMethod(cfg_but, MUIM_Notify, MUIA_Pressed, FALSE, config_win, 3, MUIM_Set, MUIA_Window_Open, TRUE);
  609.     DoMethod(quit_but, MUIM_Notify, MUIA_Pressed, FALSE, app, 2, MUIM_Application_ReturnID, MUIV_Application_ReturnID_Quit);
  610.  
  611. /* Respond to buttons in the name search popup */
  612.     DoMethod(search_str, MUIM_Notify, MUIA_String_Acknowledge, MUIV_EveryTime, app, 2, MUIM_Application_ReturnID, ID_Name);
  613.     DoMethod(search_next, MUIM_Notify, MUIA_Pressed, FALSE, app, 2, MUIM_Application_ReturnID, ID_Name);
  614.     DoMethod(search_can, MUIM_Notify, MUIA_Pressed, FALSE, name_but, 2, MUIM_Popstring_Close, TRUE);
  615.  
  616. /* Respond to double clicking in one of the file listviews */
  617.     DoMethod(recentlv, MUIM_Notify, MUIA_Listview_DoubleClick,TRUE, app, 2, MUIM_Application_ReturnID, ID_Readme);
  618.     DoMethod(shortlv, MUIM_Notify, MUIA_Listview_DoubleClick,TRUE, app, 2, MUIM_Application_ReturnID, ID_Readme);
  619.     DoMethod(newlv, MUIM_Notify, MUIA_Listview_DoubleClick,TRUE, app, 2, MUIM_Application_ReturnID, ID_Readme);
  620.     DoMethod(taggedlv, MUIM_Notify, MUIA_Listview_DoubleClick,TRUE, app, 2, MUIM_Application_ReturnID, ID_Readme);
  621.  
  622. /* Respond to the readme page buttons */
  623.     DoMethod(read_next_but, MUIM_Notify, MUIA_Pressed, FALSE, app, 2, MUIM_Application_ReturnID, ID_ReadNext);
  624.     DoMethod(read_can_but, MUIM_Notify, MUIA_Pressed, FALSE, mainpage, 3, MUIM_Set, MUIA_Group_ActivePage, FILE_PAGE);
  625.  
  626. /* Respond to the download abort button */
  627.     DoMethod(get_abort_but, MUIM_Notify, MUIA_Pressed, FALSE, app, 2, MUIM_Application_ReturnID, ID_GetAbort);
  628. /**/DoMethod(get_abort_but, MUIM_Notify, MUIA_Pressed, FALSE, mainpage, 3, MUIM_Set, MUIA_Group_ActivePage, FILE_PAGE);
  629.  
  630. /* Respond to events in the hosts window */
  631.     DoMethod(hosts_win, MUIM_Window_SetCycleChain, hostslv, host_add_but, host_del_but, host_connect_but, host_cancel_but, NULL);
  632.     set(hosts_win, MUIA_Window_ActiveObject, hostslv);
  633.     DoMethod(hosts_win, MUIM_Notify, MUIA_Window_CloseRequest, TRUE, hosts_win, 3, MUIM_Set, MUIA_Window_Open, FALSE);
  634.     DoMethod(hostslv, MUIM_Notify, MUIA_Listview_DoubleClick,TRUE, app, 2, MUIM_Application_ReturnID, ID_HostsConnect);
  635.     DoMethod(host_add_but, MUIM_Notify, MUIA_Pressed, FALSE, add_host_win, 3, MUIM_Set, MUIA_Window_Open, TRUE);
  636.     DoMethod(host_del_but, MUIM_Notify, MUIA_Pressed, FALSE, hostslv, 2, MUIM_List_Remove, MUIV_List_Remove_Active);
  637.     DoMethod(host_connect_but, MUIM_Notify, MUIA_Pressed, FALSE, app, 2, MUIM_Application_ReturnID, ID_HostsConnect);
  638.     DoMethod(host_cancel_but, MUIM_Notify, MUIA_Pressed, FALSE, hosts_win, 3, MUIM_Set, MUIA_Window_Open, FALSE);
  639.  
  640. /* Respond to events in the add a host window */
  641.     DoMethod(add_host_win, MUIM_Window_SetCycleChain, host_add_country, host_add_site, host_add_dir, host_add_done, NULL);
  642.  
  643.     DoMethod(add_host_win, MUIM_Notify, MUIA_Window_CloseRequest, TRUE, add_host_win, 3, MUIM_Set, MUIA_Window_Open, FALSE);
  644.     DoMethod(host_add_done, MUIM_Notify, MUIA_Pressed, FALSE, add_host_win, 3, MUIM_Set, MUIA_Window_Open, FALSE);
  645.     DoMethod(host_add_done, MUIM_Notify, MUIA_Pressed, FALSE, app, 2, MUIM_Application_ReturnID, ID_AddHost);
  646.  
  647. /* Respond to the config page buttons */
  648.     DoMethod(config_win, MUIM_Window_SetCycleChain, def_sort_but, def_view_but, def_getdir, def_tmpdir, def_uncomp, def_done, NULL);
  649.     DoMethod(config_win, MUIM_Notify, MUIA_Window_CloseRequest, TRUE, config_win, 3, MUIM_Set, MUIA_Window_Open, 0);
  650.     DoMethod(def_done, MUIM_Notify, MUIA_Pressed, FALSE, app, 2, MUIM_Application_ReturnID, ID_Config);
  651.  
  652. /* Get Prefs */
  653.     if (read_prefs()) adt_status("Welcome new user.");
  654.     else adt_status("Welcome.");
  655.  
  656. /* Start the window up then */
  657.     DoMethod(win, MUIM_Notify, MUIA_Window_CloseRequest, TRUE, app, 2, MUIM_Application_ReturnID, MUIV_Application_ReturnID_Quit);
  658.     set(win, MUIA_Window_Open, TRUE);
  659.  
  660.     while (running) {
  661.     ULONG signal;
  662.  
  663.     switch (DoMethod(app, MUIM_Application_Input, &signal))
  664.     {
  665.         case MUIV_Application_ReturnID_Quit:
  666.         running = FALSE;
  667.         break;
  668.  
  669.         case ID_Sort: {
  670.         int page;
  671.  
  672.         sortrecent = sortshort = sortnew = sorttagged = TRUE;
  673.         get(sort_but, MUIA_Cycle_Active, &sorttype);
  674.         get(lvpage, MUIA_Group_ActivePage, &page);
  675.         switch(page) {
  676.             case FILE_RECENT_PAGE:
  677.             DoMethod(recentlv, MUIM_List_Sort);
  678.             sortrecent = FALSE;
  679.             break;
  680.             case FILE_ALL_PAGE:
  681.             DoMethod(shortlv, MUIM_List_Sort);
  682.             sortshort = FALSE;
  683.             break;
  684.             case FILE_TAGGED_PAGE:
  685.             DoMethod(taggedlv, MUIM_List_Sort);
  686.             sorttagged = FALSE;
  687.             break;
  688.             case FILE_NEW_PAGE:
  689.             DoMethod(newlv, MUIM_List_Sort);
  690.             sortnew = FALSE;
  691.             break;
  692.         }
  693.         break;
  694.         }
  695.  
  696.         case ID_View:
  697.         if (connected) {
  698.             get(view_but, MUIA_Cycle_Active, &viewtype);
  699.             switch(viewtype) {
  700.             case 0:
  701.                 show_new();
  702.                 break;
  703.  
  704.             case 1:
  705.                 show_recent();
  706.                 break;
  707.  
  708.             case 2:
  709.                 show_short();
  710.                 break;
  711.  
  712.             case 3:
  713.                 show_tagged();
  714.                 break;
  715.             }
  716.         } else adt_status("Not Connected");
  717.         set(win, MUIA_Window_ActiveObject, currentlv());
  718.         break;
  719.  
  720.         case ID_Readme: {
  721.         APTR curntlv;
  722.         struct FileLineRec *flr;
  723.         readpos = -1;
  724.         curntlv = currentlv();
  725.         DoMethod(curntlv, MUIM_List_NextSelected, &readpos);
  726.         if (readpos == -1) break;
  727.         DoMethod(curntlv, MUIM_List_GetEntry, readpos, &flr);
  728.         if (HAS_README(flr)) readme(flr->dir, flr->readme);
  729.         break;
  730.         }
  731.  
  732.         case ID_ReadNext: {
  733.         APTR curntlv;
  734.         struct FileLineRec *flr;
  735.  
  736.         curntlv = currentlv();
  737.         DoMethod(curntlv, MUIM_List_NextSelected, &readpos);
  738.         if (readpos == -1) {
  739.             set(mainpage, MUIA_Group_ActivePage, FILE_PAGE);
  740.             break;
  741.         }
  742.         DoMethod(curntlv, MUIM_List_GetEntry, readpos, &flr);
  743.         if (HAS_README(flr)) readme(flr->dir, flr->readme);
  744.         break;
  745.         }
  746.  
  747.         case ID_Name: {
  748.         LONG pos;
  749.         struct FileLineRec *flr;
  750.         char *str;
  751.         register BOOL stop = FALSE;
  752.         register int srclen, destlen;
  753.         register int i;
  754.  
  755.         get(search_str, MUIA_String_Contents, &str);
  756.         srclen = strlen(str);
  757.         get(currentlv(), MUIA_List_Active, &pos);
  758.         if (pos < 0) pos = 0;
  759.         else pos++;
  760.         for(;!stop;pos++) {
  761.             DoMethod(currentlv(), MUIM_List_GetEntry, pos, &flr);
  762.             if (!flr) break;
  763.             destlen = strlen(flr->name);
  764.             for (i=0;i<=destlen-srclen&&!stop;i++) {
  765.             if (!strnicmp(str, &flr->name[i], srclen)) {
  766.                 set(currentlv(), MUIA_List_Active, pos);
  767.                 stop=TRUE;
  768.             }
  769.             }
  770.         }
  771.         break;
  772.         }
  773.  
  774.         case ID_Get: {
  775.         LONG page, pos=-1, pos2;
  776.         struct FileLineRec *flr, *flr2;
  777.         BOOL stop=FALSE;
  778.         int result=0;
  779.  
  780.         if (connected) {
  781.             DoMethod(currentlv(), MUIM_List_NextSelected, &pos);
  782.             if (pos == -1) break;
  783.             DoMethod(getlv, MUIM_List_Clear, NULL);
  784.             get(lvpage, MUIA_Group_ActivePage, &page);
  785.             switch(page) {
  786.             case FILE_RECENT_PAGE:
  787.                 copy_tagged(getlv); break;
  788.             case FILE_ALL_PAGE:
  789.                 copy_tagged(getlv); break;
  790.             case FILE_TAGGED_PAGE:
  791.                 copy_tagged(getlv); break;
  792.             case FILE_NEW_PAGE:
  793.                 copy_tagged(getlv); break;
  794.             }
  795.             set(mainpage, MUIA_Group_ActivePage, GET_PAGE);
  796.  
  797.             pos = 0;
  798.             while(result != 2) {
  799.             char file[256], local[256];
  800.  
  801.             DoMethod(getlv, MUIM_List_GetEntry, pos, &flr);
  802.             if (!flr) break;
  803.             joinpaths(file, flr->dir, flr->name);
  804.             joinpaths(local, prefs.getdir, flr->name);
  805.             result = get_file(file, local, flr->intsize);
  806.             if (!result) {
  807.                 stop = FALSE;
  808.                 pos2 = -1;
  809.                 while(!stop) {
  810.                 DoMethod(currentlv(), MUIM_List_NextSelected, &pos2);
  811.                 if (pos2 == -1) break;
  812.                 DoMethod(currentlv(), MUIM_List_GetEntry, pos2, &flr2);
  813.                 if (flr2->time == flr->time) {
  814.                     DoMethod(currentlv(), MUIM_List_Select, pos2, MUIV_List_Select_Off, NULL);
  815.                     stop = TRUE;
  816.                 }
  817.                 }
  818.                 DoMethod(getlv, MUIM_List_Remove, pos);
  819.             }
  820.             else {
  821. /*                  printf("error getting %s\n", file); */
  822.                 pos++;
  823.             }
  824.             }
  825.             /* Copy remaining entries back to lvpage */
  826.             set(mainpage, MUIA_Group_ActivePage, FILE_PAGE);
  827.         }
  828.         break;
  829.         }
  830.  
  831.         case ID_HostsConnect: {
  832.         char *error;
  833.         LONG pos=-1;
  834.         struct HostLineRec *hlr;
  835.  
  836.         /* Connect to a host and download the default file list */
  837.         DoMethod(hostslv, MUIM_List_NextSelected, &pos);
  838.         if (pos >= 0) {
  839.             set(win, MUIA_Window_Sleep, TRUE);
  840.             set(hosts_win, MUIA_Window_Open, FALSE);
  841.             gotrecent = gotshort = gotnew = FALSE;
  842.             DoMethod(recentlv, MUIM_List_Clear, NULL);
  843.             DoMethod(newlv, MUIM_List_Clear, NULL);
  844.             DoMethod(shortlv, MUIM_List_Clear, NULL);
  845.             {
  846.             char file[256];
  847.             joinpaths(file, prefs.tmpdir, "RECENT.adt");
  848.             remove(file);
  849.             joinpaths(file, prefs.tmpdir, "SHORT.adt");
  850.             remove(file);
  851.             }
  852.  
  853.             pos=-1;
  854.             DoMethod(hostslv, MUIM_List_NextSelected, &pos);
  855.             DoMethod(hostslv, MUIM_List_GetEntry, pos, &hlr);
  856.             adt_status("Connecting");
  857.             if (!(error = ftp_connect(hlr->host, hlr->dir))) {
  858.             adt_status("Connected");
  859.             DoMethod(app, MUIM_Application_ReturnID, ID_View);
  860.             set(mainpage, MUIA_Group_ActivePage, FILE_PAGE);
  861.             host = hlr;
  862.             } else if (error[0] == '*') {
  863.             adt_status(error+1);
  864.             fail(app, error+1);
  865.             } else adt_status(error);
  866.             set(win, MUIA_Window_Sleep, FALSE);
  867.         }
  868.         break;
  869.         }
  870.  
  871.         case ID_Config: {
  872.         char *tmp;
  873.         get(def_sort_but, MUIA_Cycle_Active, &prefs.defsort);
  874.         get(def_view_but, MUIA_Cycle_Active, &prefs.defview);
  875.         get(def_getdir, MUIA_String_Contents, &tmp);
  876.         strcpy(prefs.getdir, tmp);
  877.         get(def_tmpdir, MUIA_String_Contents, &tmp);
  878.         strcpy(prefs.tmpdir, tmp);
  879.         get(def_uncomp, MUIA_String_Contents, &tmp);
  880.         strcpy(prefs.uncomp, tmp);
  881.         set(config_win, MUIA_Window_Open, FALSE);
  882.         break;
  883.         }
  884.  
  885.         case ID_AddHost: {
  886.         char line[100], *country, *site, *dir;
  887.  
  888.         get(host_add_country, MUIA_String_Contents, &country);
  889.         get(host_add_site, MUIA_String_Contents, &site);
  890.         get(host_add_dir, MUIA_String_Contents, &dir);
  891.         sprintf(line, "%s:%s:%s", country, site, dir);
  892.         DoMethod(hostslv, MUIM_List_InsertSingle, line, MUIV_List_Insert_Bottom);
  893.         set(host_add_country, MUIA_String_Contents, "");
  894.         set(host_add_site, MUIA_String_Contents, "");
  895.         set(host_add_dir, MUIA_String_Contents, "");
  896.         }
  897.  
  898.     }
  899.     if (signal) Wait(signal);
  900.     }
  901.     {
  902.     char file[256];
  903.     joinpaths(file, prefs.tmpdir, "RECENT.adt");
  904.     remove(file);
  905.     joinpaths(file, prefs.tmpdir, "SHORT.adt");
  906.     remove(file);
  907.     }
  908.     if (write_prefs(prefs.defcfg)) MUI_Request(app,win,0,NULL,"OK", "Can't save prefs.");
  909.  
  910. /* Save to envarc: if defcfg is env: */
  911.     if (!strnicmp(prefs.defcfg, "env:", 4)) {
  912.     char file[256];
  913.  
  914.     sprintf(file, "envarc:%s", prefs.defcfg + 4);
  915.     if (write_prefs(file)) MUI_Request(app,win,0,NULL,"OK", "Can't save prefs to ENVARC:");
  916.     }
  917.  
  918.     fail(app, NULL);
  919. }
  920.  
  921.  
  922. void adt_status(char *msg)
  923. {
  924.     set(adt_statusobj, MUIA_Text_Contents, msg);
  925. }
  926.  
  927.  
  928. /*------------- config file format ----------------
  929. # comment
  930. server = <default server>
  931. prevcall = <time of last call>
  932. getdir = <download directory>
  933. tmpdir = <temporary directory>
  934. defsort = <default sort method (integer)>
  935. defview = <default view method (integer)>
  936. uncompress = <uncompress command>
  937. site = <country> <site> <directory>
  938. */
  939.  
  940. int read_prefs()
  941. {
  942.     FILE *fp;
  943.     char line[256], *tag;
  944.     int length, retval;
  945.  
  946.     if (fp = fopen(prefs.defcfg, "r")) {
  947.     retval = 0;
  948.     while (fgets(line, 256, fp)) {
  949.         if (line[0] != '#') {
  950.         length = strlen(line);
  951.         if (line[length-1] == '\n') line[length-1] = '\0';
  952.         for (tag=line; *tag!='=' && *tag!='\0'; tag++);
  953.         tag++;
  954.         for (; *tag==' '; tag++);
  955.         if (*tag == '\0') continue;
  956.  
  957.         if (!strnicmp(line, "server", 6))
  958.             strcpy(prefs.server, tag);
  959.         if (!strnicmp(line, "prevcall", 8))
  960.             sscanf(tag, "%lx", &prefs.lasttime);
  961.         if (!strnicmp(line, "getdir", 6))
  962.             strcpy(prefs.getdir, tag);
  963.         if (!strnicmp(line, "tmpdir", 6))
  964.             strcpy(prefs.tmpdir, tag);
  965.         if (!strnicmp(line, "defsort", 7))
  966.             sscanf(tag, "%lx", &prefs.defsort);
  967.         if (!strnicmp(line, "defview", 7))
  968.             sscanf(tag, "%lx", &prefs.defview);
  969.         if (!strnicmp(line, "uncompress", 10))
  970.             strcpy(prefs.uncomp, tag);
  971.         if (!strnicmp(line, "site", 4))
  972.             DoMethod(hostslv, MUIM_List_InsertSingle, tag, MUIV_List_Insert_Bottom);
  973.  
  974.         }
  975.     }
  976.     fclose(fp);
  977.     } else {
  978.        int i;
  979.  
  980. /* If no config file is found, use the default host sites. */
  981.        for (i=0;DefHostList[i]!=NULL;i++) {
  982.        DoMethod(hostslv, MUIM_List_InsertSingle, DefHostList[i], MUIV_List_Insert_Bottom);
  983.        }
  984.        retval = 1;
  985.     }
  986.  
  987.     prefs.thistime = prefs.lasttime;
  988.     set(sort_but, MUIA_Cycle_Active, prefs.defsort);
  989.     set(view_but, MUIA_Cycle_Active, prefs.defview);
  990.     set(def_sort_but, MUIA_Cycle_Active, prefs.defsort);
  991.     set(def_view_but, MUIA_Cycle_Active, prefs.defview);
  992.     set(def_getdir, MUIA_String_Contents, prefs.getdir);
  993.     set(def_tmpdir, MUIA_String_Contents, prefs.tmpdir);
  994.     set(def_uncomp, MUIA_String_Contents, prefs.uncomp);
  995.     return(retval);
  996. }
  997.  
  998. int write_prefs(char *file)
  999. {
  1000.     FILE *fp;
  1001.     struct HostLineRec *hlr;
  1002.     int i;
  1003.  
  1004.     if (!(fp = fopen(file, "w"))) return(1);
  1005.     fputs("# This is a MUIAdt config file\n", fp);
  1006.     fprintf(fp, "server = %s\n", prefs.server);
  1007.     fprintf(fp, "prevcall = %lx\n", prefs.thistime);
  1008.     fprintf(fp, "getdir = %s\n", prefs.getdir);
  1009.     fprintf(fp, "tmpdir = %s\n", prefs.tmpdir);
  1010.     fprintf(fp, "defsort = %lx\n", prefs.defsort);
  1011.     fprintf(fp, "defview = %lx\n", prefs.defview);
  1012.     fprintf(fp, "uncompress = %s\n", prefs.uncomp);
  1013.     for(i=0;;i++)
  1014.     {
  1015.     DoMethod(hostslv, MUIM_List_GetEntry, i, &hlr);
  1016.     if (!hlr) break;
  1017.     fprintf(fp, "site = %s:%s:%s\n", hlr->country, hlr->host, hlr->dir);
  1018.     }
  1019.     fclose(fp);
  1020.     return(0);
  1021. }
  1022.  
  1023. void joinpaths(char *str, char *path, char *file)
  1024. {
  1025.     if (strlen(path)) {
  1026.     if (path[strlen(path)-1] == '/' || path[strlen(path)-1] == ':')
  1027.         sprintf(str, "%s%s", path, file);
  1028.     else
  1029.         sprintf(str, "%s/%s", path, file);
  1030.     } else strcpy(str, file);
  1031. }
  1032.  
  1033. int get_recent(void)
  1034. {
  1035.     int retval=0;
  1036.     char local[256], command[256];
  1037.  
  1038.     set(recentlv, MUIA_List_Quiet, TRUE);
  1039.     joinpaths(local, prefs.tmpdir, "RECENT.adt.Z");
  1040.     adt_status("Downloading RECENT list");
  1041.     if (!get_file("info/adt/RECENT.adt.Z", local, 0)) {
  1042.     sprintf(command, "%s \"%s\"", prefs.uncomp, local);
  1043.     system(command);
  1044.     local[strlen(local)-2] = '\0';
  1045.     if (get_parse(local, recentlv) == 0) gotrecent=TRUE;
  1046.     else retval = 2;
  1047.     } else {
  1048.     adt_status("Error downloading RECENT list");
  1049.     retval = 1;
  1050.     }
  1051.     return(retval);
  1052. }
  1053.  
  1054. void show_new()
  1055. {
  1056.     LONG pos, numfiles;
  1057.     struct FileLineRec *flr;
  1058.     char temp[20], line[256];
  1059.  
  1060.     set(win, MUIA_Window_Sleep, TRUE);
  1061.     set(newlv, MUIA_List_Quiet, TRUE);
  1062.     if (!gotrecent) get_recent();
  1063.     if (gotrecent) {
  1064.     if (!gotnew) {
  1065.         for(pos=0;;pos++) {
  1066.         DoMethod(recentlv, MUIM_List_GetEntry, pos, &flr);
  1067.         if (!flr) break;
  1068.         if (flr->time > prefs.lasttime) {
  1069.             sprintf(line, "%lx@%s@%s;%lx@%s;@%s\n", flr->time, flr->dir, flr->name, flr->intsize, flr->readme, flr->descr);
  1070.             DoMethod(newlv, MUIM_List_InsertSingle, line, MUIV_List_Insert_Bottom);
  1071.         }
  1072.         }
  1073.         gotnew = TRUE;
  1074.     }
  1075.     if (sortnew) {
  1076.         DoMethod(newlv, MUIM_List_Sort, NULL);
  1077.         sortnew = FALSE;
  1078.     }
  1079.     clone_tagged(newlv);
  1080.     get(newlv, MUIA_List_Entries, &numfiles);
  1081.     sprintf(temp, "New files (%d)", numfiles);
  1082.     adt_status(temp);
  1083.     }
  1084.     set(lvpage, MUIA_Group_ActivePage, FILE_NEW_PAGE);
  1085.     set(newlv, MUIA_List_Quiet, FALSE);
  1086.     set(recentlv, MUIA_List_Quiet, FALSE);
  1087.     set(win, MUIA_Window_Sleep, FALSE);
  1088. }
  1089.  
  1090. void show_recent()
  1091. {
  1092.     LONG numfiles;
  1093.     char temp[20];
  1094.  
  1095.     set(win, MUIA_Window_Sleep, TRUE);
  1096.     if (!gotrecent) get_recent();
  1097.     if (gotrecent) {
  1098.     if (sortrecent) {
  1099.         DoMethod(recentlv, MUIM_List_Sort, NULL);
  1100.         sortrecent = FALSE;
  1101.     }
  1102.     clone_tagged(recentlv);
  1103.  
  1104.     get(recentlv, MUIA_List_Entries, &numfiles);
  1105.     sprintf(temp, "Recent files (%d)", numfiles);
  1106.     adt_status(temp);
  1107.     }
  1108.     set(lvpage, MUIA_Group_ActivePage, FILE_RECENT_PAGE);
  1109.     set(recentlv, MUIA_List_Quiet, FALSE);
  1110.     set(win, MUIA_Window_Sleep, FALSE);
  1111. }
  1112.  
  1113. void show_short()
  1114. {
  1115.     LONG numfiles;
  1116.     char temp[20];
  1117.  
  1118.     set(win, MUIA_Window_Sleep, TRUE);
  1119.     set(shortlv, MUIA_List_Quiet, TRUE);
  1120.     if (!gotshort) {
  1121.     char local[256], command[256];
  1122.  
  1123.     joinpaths(local, prefs.tmpdir, "SHORT.adt.Z");
  1124.     adt_status("Downloading LONG list");
  1125.     if (!get_file("info/adt/SHORT.adt.Z", local, 0)) {
  1126.         sprintf(command, "%s \"%s\"", prefs.uncomp, local);
  1127.         system(command);
  1128.         local[strlen(local)-2] = '\0';
  1129.         if(!get_parse(local, shortlv)) gotshort=TRUE;
  1130.     } else adt_status("Error downloading LONG list");
  1131.     }
  1132.     if (gotshort) {
  1133.     if (sortshort) {
  1134.         DoMethod(shortlv, MUIM_List_Sort, NULL);
  1135.         sortshort = FALSE;
  1136.     }
  1137.     clone_tagged(shortlv);
  1138.  
  1139.     get(shortlv, MUIA_List_Entries, &numfiles);
  1140.     sprintf(temp, "All files (%d)", numfiles);
  1141.     adt_status(temp);
  1142.     }
  1143.     set(lvpage, MUIA_Group_ActivePage, FILE_ALL_PAGE);
  1144.     set(shortlv, MUIA_List_Quiet, FALSE);
  1145.     set(win, MUIA_Window_Sleep, FALSE);
  1146. }
  1147.  
  1148. void show_tagged()
  1149. {
  1150.     char temp[40];
  1151.     LONG numfiles;
  1152.  
  1153.     set(taggedlv, MUIA_List_Quiet, TRUE);
  1154.     DoMethod(taggedlv, MUIM_List_Clear, NULL);
  1155.     copy_tagged(taggedlv);
  1156.     if (sorttagged) {
  1157.     DoMethod(taggedlv, MUIM_List_Sort, NULL);
  1158.     sorttagged = FALSE;
  1159.     }
  1160.     set(lvpage, MUIA_Group_ActivePage, FILE_TAGGED_PAGE);
  1161.     set(taggedlv, MUIA_List_Quiet, FALSE);
  1162.  
  1163.     get(taggedlv, MUIA_List_Entries, &numfiles);
  1164.     sprintf(temp, "Tagged files (%d)", numfiles);
  1165.     adt_status(temp);
  1166. }
  1167.  
  1168. void readme(char *dir, char *name)
  1169. {
  1170.     char fullpath[256], local[256], *buffer;
  1171.     int fh;
  1172.     struct stat st;
  1173.     char adt_status_text[100], temp[100], *ptr;
  1174.  
  1175.     set(win, MUIA_Window_Sleep, TRUE);
  1176.     get(adt_statusobj, MUIA_Text_Contents, &ptr);
  1177.     strcpy(adt_status_text, ptr);
  1178.     sprintf(temp, "Downloading %s", name);
  1179.     adt_status(temp);
  1180.  
  1181. /* download readme and put it in the readme page */
  1182.     joinpaths(fullpath, dir, name);
  1183.     joinpaths(local, prefs.tmpdir, name);
  1184.  
  1185.     if (!get_file(fullpath, local, 0) && (fh = open(local, O_RDONLY))) {
  1186.     stat(local, &st);
  1187.     if (buffer = AllocMem(st.st_size, MEMF_ANY|MEMF_CLEAR)) {
  1188.         read(fh, buffer, st.st_size);
  1189.         set(readtext, MUIA_Floattext_Text, buffer);
  1190.         set(mainpage, MUIA_Group_ActivePage, README_PAGE);
  1191.  
  1192.         close(fh);
  1193.         FreeMem(buffer, st.st_size);
  1194.     }
  1195.     }
  1196.     remove(local);
  1197.     adt_status(adt_status_text);
  1198.     set(win, MUIA_Window_Sleep, FALSE);
  1199. }
  1200.  
  1201. APTR currentlv()
  1202. {
  1203.     LONG page;
  1204.  
  1205.     get(lvpage, MUIA_Group_ActivePage, &page);
  1206.     switch(page) {
  1207.     case FILE_RECENT_PAGE:
  1208.         return(recentlv); break;
  1209.     case FILE_ALL_PAGE:
  1210.         return(shortlv); break;
  1211.     case FILE_TAGGED_PAGE:
  1212.         return(taggedlv); break;
  1213.     case FILE_NEW_PAGE:
  1214.         return(newlv); break;
  1215.     }
  1216. }
  1217.  
  1218. int get_file(char *dir, char *file, int size)
  1219. {
  1220.     return(ftp_get(dir, size, file));
  1221. }
  1222.  
  1223. static APTR beforetagged=NULL;
  1224.  
  1225. void clone_tagged(APTR destlv)
  1226. {
  1227.     APTR curntlv;
  1228.     LONG pos=-1;
  1229.     struct FileLineRec *srcflr, *destflr;
  1230.  
  1231.     curntlv = currentlv();
  1232.  
  1233. /* Clone tagged from the previous listview if switching from tagged lv
  1234.   (because tagged lines will not hold same positions relative to other lv's) */
  1235.  
  1236.     if (curntlv == taggedlv && destlv != taggedlv) curntlv = beforetagged;
  1237.     if (curntlv != destlv) {
  1238.     DoMethod(destlv, MUIM_List_Select, MUIV_List_Select_All, MUIV_List_Select_Off, NULL);
  1239.     for (;;) {
  1240.         DoMethod(curntlv, MUIM_List_NextSelected, &pos);
  1241.         if (pos == -1) break;
  1242.         DoMethod(curntlv, MUIM_List_GetEntry, pos, &srcflr);
  1243.         DoMethod(destlv, MUIM_List_GetEntry, pos, &destflr);
  1244.         if (destflr && (srcflr->time == destflr->time)) {
  1245.         DoMethod(destlv, MUIM_List_Select, pos, MUIV_List_Select_On, NULL);
  1246.         }
  1247.     }
  1248.     }
  1249. }
  1250.  
  1251. void copy_tagged(APTR destlv)
  1252. {
  1253.     APTR curntlv;
  1254.     LONG pos=-1;
  1255.     struct FileLineRec *flr;
  1256.     char line[256];
  1257.  
  1258.     curntlv = currentlv();
  1259.     if (destlv == taggedlv) beforetagged = curntlv;
  1260.     for (;;) {
  1261.     DoMethod(curntlv, MUIM_List_NextSelected, &pos);
  1262.     if (pos == -1) break;
  1263.     DoMethod(curntlv, MUIM_List_GetEntry, pos, &flr);
  1264.     sprintf(line, "%lx@%s@%s;%lx@%s;@%s\n", flr->time, flr->dir, flr->name, flr->intsize, flr->readme, flr->descr);
  1265.     DoMethod(destlv, MUIM_List_InsertSingle, line, MUIV_List_Insert_Bottom);
  1266.     }
  1267. }
  1268.  
  1269. void copy_all(APTR destlv)
  1270. {
  1271.     APTR curntlv;
  1272.     LONG pos;
  1273.     struct FileLineRec *flr;
  1274.     char line[256];
  1275.  
  1276.     curntlv =    currentlv();
  1277.     for (pos=0;;pos++){
  1278.     DoMethod(curntlv, MUIM_List_GetEntry, pos, &flr);
  1279.     if (!flr) break;
  1280.     sprintf(line, "%lx@%s@%s;%lx@%s;@%s\n", flr->time, flr->dir, flr->name, flr->intsize, flr->readme, flr->descr);
  1281.     DoMethod(destlv, MUIM_List_InsertSingle, line, MUIV_List_Insert_Bottom);
  1282.     }
  1283. }
  1284.  
  1285. int get_parse(char *file, APTR lv)
  1286. {
  1287.     FILE *fp;
  1288.     char temp[40];
  1289.     int version=0, err=0;
  1290.  
  1291.     DoMethod(lv, MUIM_List_Clear, NULL);
  1292.     adt_status("Parsing file list");
  1293.     if (!(fp = fopen(file, "r"))) {
  1294.     adt_status("Error: Uncompress failed");
  1295.     return(1);
  1296.     }
  1297.  
  1298.     if (fscanf(fp, "adt-v%d\n", &version) != 1) {
  1299.     fclose(fp);
  1300.     sprintf(temp, "Error parsing %s file list", file);
  1301.     adt_status(temp);
  1302.     return(1);
  1303.     }
  1304.  
  1305. /*    switch(version) {
  1306.     case 1: */
  1307.         err = adt_parse_v1(fp, lv);
  1308. /*          break;
  1309.     } */
  1310.  
  1311.     fclose(fp);
  1312.     return(err);
  1313. }
  1314.  
  1315.  
  1316. int adt_parse_v1(FILE *in, APTR lv)
  1317. {
  1318.     char lnbuf[256];
  1319.  
  1320.     while (fgets(lnbuf, 256, in))
  1321.     DoMethod(lv, MUIM_List_InsertSingle, lnbuf, MUIV_List_Insert_Bottom);
  1322.     return(0);
  1323. }
  1324.  
  1325.